package cn.li.chatgpt.api.jop;

import cn.li.chatgpt.api.AiServiceImp;
import cn.li.chatgpt.api.AnswerApi;
import cn.li.chatgpt.api.zsmodel.resp.Resp_data;
import cn.li.chatgpt.api.zsmodel.resp.UnAnswerResponse;
import cn.li.chatgpt.api.zsmodel.vo.Topics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.io.IOException;

@Slf4j
@Configuration
public class AnswerSchedule {
    @Value("${data.cookie}")
    private String cookie;
    @Value("${data.groupId}")
    private String groupId;
    @Resource
    private AiServiceImp aiService;
    @Resource
    private AnswerApi answerService;

    @Scheduled(cron = "0/20 * * * * ?" )
    public void answerWork(){
        log.info("开始回答问题");
        log.info("cookie:{}",cookie);
        log.info("groupId:{}",groupId);
        try {
            UnAnswerResponse unAnswerResponse = answerService.queryQuestion(cookie, groupId);
            Resp_data resp_data = unAnswerResponse.getResp_data();
            if(!unAnswerResponse.getSucceeded()||resp_data.getTopics().size()==0) return;
            for(Topics topics:resp_data.getTopics()){
                String s = aiService.aiAnswer(topics.getQuestion().getText());
                answerService.answer(cookie,topics.getTopic_id(),true,s);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


}
